$(function () {
    var div = $("<div id=\"currentInfo\">\n    <div class=\"p p1\">\u4ECA\u5929\u662F<span class=\"num year\"></span>\u5E74<span class=\"num month\"></span>\u6708<span class=\"num day\"></span>\u65E5</div>\n    <div class=\"p p2\">\u661F\u671F</div>\n    <div class=\"p p3\"><span class=\"num time\"></span></div>\n    <div class=\"p p4\"></div>\n</div>"); // eslint-disable-next-line

    Date.prototype.Format = function (fmt) {
      var o = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S": this.getMilliseconds()
      };
    
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
      }
    
      for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
          fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
      }
    
      return fmt;
    };
    
    $('#weather').html(div);
    var DAT_ARRAY = ['日', '一', '二', '三', '四', '五', '六'];
    
    var spaceBetween = function spaceBetween(v) {
      return ' ' + v + ' ';
    };
    
    var updateCurrentInfo = function updateCurrentInfo() {
      var date = new Date();
      div.find('.year').text(spaceBetween(date.getFullYear()));
      div.find('.month').text(spaceBetween(date.getMonth() + 1));
      div.find('.day').text(spaceBetween(date.getDate()));
      div.find('.p2').text('星期' + DAT_ARRAY[date.getDay()]);
      div.find('.time').text(date.Format("hh:mm"));
    };
    
    // var updateTemperature = function updateTemperature() {
    //   AMap.plugin('AMap.Weather', function () {
    //     var weather = new AMap.Weather();
    //     weather.getLive(320281, function (err, data) {
    //       if (err) {
    //         div.find('.p4').text('--');
    //       } else {
    //         div.find('.p4').text(data.weather + "/" + data.temperature + '°C');
    //       }
    //     });
    //   });
    // };
    
    updateCurrentInfo();
    //updateTemperature(); // 一分钟更新一次时间（会有0~59秒误差）
    
    setInterval(updateCurrentInfo, 60000); // 十分钟更新一次天气
    
    //setInterval(updateTemperature, 600000);
    
});
